package com.xt.base.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Compressor {
	
    private static final int BUFSIZ = 4096;
    
	public static String zip(String input){
		try{ 
			ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			GZIPOutputStream gzout=new GZIPOutputStream(out); 
			gzout.write(input.getBytes("UTF-8")); 
			gzout.close(); 
	        return out.toString("ISO_8859_1");
		}catch(IOException e) { 
			e.printStackTrace();
			return "";
		} 
	}
	
	public static String unzip(String input)throws	IOException{
		byte[] zipBytes = input.getBytes("ISO_8859_1");
		ByteArrayInputStream bais = new ByteArrayInputStream(zipBytes);   
		GZIPInputStream  zis = new GZIPInputStream(bais);   
        ByteArrayOutputStream baos = new ByteArrayOutputStream();   
        byte inbuf[] = new byte[BUFSIZ];   
        int n;   
        while((n = zis.read(inbuf,0,BUFSIZ)) != -1)baos.write(inbuf,0,n);   
        String res = baos.toString("UTF-8");
        zis.close();   
        return   res;   
	}

	public static byte[] zipArray(byte[] zipBytes){
		try{ 
			ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			GZIPOutputStream gzout=new GZIPOutputStream(out); 
			gzout.write(zipBytes); 
			gzout.close(); 
	        return out.toByteArray();
		}catch(IOException e) { 
			e.printStackTrace();
			return null;
		} 
	}
	
	public static byte[] zipArray(String input){
		try{ 
			ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			GZIPOutputStream gzout=new GZIPOutputStream(out); 
			gzout.write(input.getBytes("UTF-8")); 
			gzout.close(); 
	        return out.toByteArray();
		}catch(IOException e) { 
			e.printStackTrace();
			return null;
		} 
	}
	
	public static byte[] unzipArray(byte[] zipBytes){
		try{
			ByteArrayInputStream bais = new ByteArrayInputStream(zipBytes);   
			GZIPInputStream  zis = new GZIPInputStream(bais);   
	        ByteArrayOutputStream baos = new ByteArrayOutputStream();   
	        byte inbuf[] = new byte[BUFSIZ];   
	        int n;   
	        while((n = zis.read(inbuf,0,BUFSIZ)) != -1)baos.write(inbuf,0,n);   
	        byte[] res = baos.toByteArray();
	        zis.close();   
	        return   res;
		}catch(IOException e){
			throw new RuntimeException(e);
		}
	}

	public static String unzip(byte[] zipBytes){
		try{
			ByteArrayInputStream bais = new ByteArrayInputStream(zipBytes);   
			GZIPInputStream  zis = new GZIPInputStream(bais);   
	        ByteArrayOutputStream baos = new ByteArrayOutputStream();   
	        byte inbuf[] = new byte[BUFSIZ];   
	        int n;   
	        while((n = zis.read(inbuf,0,BUFSIZ)) != -1)baos.write(inbuf,0,n);   
	        String res = baos.toString("UTF-8");
	        zis.close();   
	        return   res;
		}catch(IOException e){
			throw new RuntimeException(e);
		}
	}
}
